﻿using System;
using System.Collections.Generic;
using System.Text;
using DataProvider;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class PXuat :XL_Bang
    {
        #region Cac thuoc tinh
        private string _soPX;
        public string SoPX
        {
            get { return _soPX; }
            set { _soPX = value; }
        }
        private DateTime _ngayXuat;
        public DateTime NgayXuat
        {
            get { return _ngayXuat; }
            set { _ngayXuat = value; }
        }
        private string _tenKH;
        public string TenKH
        {
            get { return _tenKH; }
            set { _tenKH = value; }
        }
        #endregion


        #region cac phuong thuc
        public DataTable GetData()
        {
            StrSQL = "select * from PXUAT";
            return GetDataTable();
        }
        public bool AddNew()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PXUAT";
                command.Parameters.AddWithValue("@SOPX", _soPX);
                command.Parameters.AddWithValue("@NGAYXUAT", _ngayXuat.ToString());
                command.Parameters.AddWithValue("@TENKH", _tenKH);
                command.Parameters.AddWithValue("@TYPE", 0);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Edit()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PXUAT";
                command.Parameters.AddWithValue("@SOPX", _soPX);
                command.Parameters.AddWithValue("@NGAYXUAT", _ngayXuat.ToString());
                command.Parameters.AddWithValue("@TENKH", _tenKH);
                command.Parameters.AddWithValue("@TYPE", 1);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Delete()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_PXUAT";
                command.Parameters.AddWithValue("@SOPX", _soPX);
                command.Parameters.AddWithValue("@NGAYXUAT", string.Empty);
                command.Parameters.AddWithValue("@TENKH", string.Empty);
                command.Parameters.AddWithValue("@TYPE", 2);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public DataTable GetData(DateTime fromDate, DateTime toDate, string selectedMaVTU)
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_TONGXUAT";
                command.Parameters.AddWithValue("@FROMDATE", fromDate);
                command.Parameters.AddWithValue("@TODATE", toDate);
                command.Parameters.AddWithValue("@MAVTU", selectedMaVTU);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                close();
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                close();
                command.Dispose();
            }
        }
        #endregion
    }
}
